﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Extractor.LibCode
{
    public class cPlayer : cDBService
    {
        public string PLAYER_HEADER_TABLE_NAME = "TBPLAYER";
        private cMapping oMapping = new cMapping();
        private string nickname = "";
        public string Nickname
        {
            get { return nickname; }
            set { nickname = value; }
        }


        private string siteid;

        public string SITEID
        {
            get { return siteid; }
            set { siteid = value; }
        }

        public cMapping.MAPPING_TYPE MAPPING_TYPE
        {
            get { return cMapping.MAPPING_TYPE.PLAYERID; }
        }

        private SITE_MAPPING site_mapping = SITE_MAPPING.SCORESWAY;
        public SITE_MAPPING SITE_MAPPING
        {
            get { return site_mapping; }
            set { site_mapping = value; }
        }
        private string localid;

        public string LOCALID
        {
            get { return localid; }
            set { localid = value; }
        }

        public cPlayer()
        {
        }

        public cPlayer(string websiteid, string slocalid)
        {
            SITEID = websiteid;
            LOCALID = slocalid;

        }

        private bool CreateNewPlayer()
        {
            DataTable dt = new DataTable();
            getTableStructure(ref dt, PLAYER_HEADER_TABLE_NAME);
            DataRow dr = dt.NewRow();
            dr["PLAYERID"] = LOCALID;
            dr["NICKNAME"] = Nickname;
            dr["RECID"] = 0;
            dr["DTCREATED"] = DateTime.Now;


            dt.Rows.Add(dr);
            string serr = "";
            try
            {
                if (Dbserver.openConn())
                {
                    bool bret = Dbserver.SqlUpdate(dt, PLAYER_HEADER_TABLE_NAME, out serr) > 0;
                    Dbserver.CommitAndCloseConn();
                    return bret;

                }
            }
            catch (Exception ex)
            {

                Dbserver.ForceRollBackNClose();
            }
            return false;
        }
        public bool savePlayer()
        {
            return savePlayer(SITEID);
        }

        public bool savePlayer(string SITE_ID)
        {
          
            LOCALID = (oMapping.getLocalCode(MAPPING_TYPE, SITE_MAPPING, SITE_ID));
            if (LOCALID != string.Empty)
            {
                if (isRecordExist(PLAYER_HEADER_TABLE_NAME,new string[]{"PLAYERID"},new string[] {LOCALID}))
                {
                    //string sql=string.Format("select * from {0} where PLAYERID='{1}'",PLAYER_HEADER_TABLE_NAME,LOCALID);
                    string serr="";
                    EditRowDtDB(out serr,PLAYER_HEADER_TABLE_NAME, new string[] { "PLAYERID" }, new string[] { LOCALID }, false, 
                        new object[] { "[SKIP]", LOCALID, Nickname,"[SKIP]", DateTime.Now });


                    
                }
                else
                {
                    return CreateNewPlayer();
                }
                

            }
            else
            {
                oMapping.CreateNewMapping(MAPPING_TYPE, SITE_MAPPING, LOCALID, SITEID);
                LOCALID = oMapping.getLocalCode(MAPPING_TYPE, SITE_MAPPING, SITEID);

                return CreateNewPlayer();
            }
            return false;

        }









    }
}
